package com.example.mobile2021606134chhnewdemo.NewsModule;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mobile2021606134chhnewdemo.R;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SearchNewsActivity extends AppCompatActivity {
    private String search;
    private RecyclerView rvSearchNews;
    private List<SearchData> list = new ArrayList<>();
    OkHttpClient okHttpClient = new OkHttpClient();
    Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_search_news);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_search_bar);
        initView();
        initTitleBar();

    }

    private void initTitleBar() {
        ImageView imgSearchBack = findViewById(R.id.imgSearchBack);
        EditText edtSearch = findViewById(R.id.edtSearch);
        View viewSearchBack = findViewById(R.id.viewSearchBack);
        View viewSearchSearch = findViewById(R.id.viewSearchSearch);
        viewSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        viewSearchSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search = edtSearch.getText().toString().trim();
                Log.i("TAG", search);
                thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("keyword", search)
                                .build();
                        Request request = new Request.Builder()
                                .addHeader("Authorization", "APPCODE " + SpeedNewsDataManager.APPCODE)
                                .url(SpeedNewsDataManager.NewsSearchURL)
                                .post(requestBody)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson = new Gson();
                                        NewsSearch newsSearch = gson.fromJson(result, NewsSearch.class);
                                        List<NewsSearch.ResultBean.ListBean> Temp = newsSearch.getResult().getList();
                                        for (int i = 0; i < Temp.size(); i++) {
                                            list.add(new SearchData(Temp.get(i).getTitle(),
                                                    Temp.get(i).getTime(),
                                                    Temp.get(i).getSrc(),
                                                    Temp.get(i).getCategory(),
                                                    Temp.get(i).getPic(),
                                                    Temp.get(i).getContent(),
                                                    Temp.get(i).getUrl(),
                                                    Temp.get(i).getWeburl()));
                                        }
                                        rvSearchNews.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                                        rvSearchNews.setAdapter(new SearchRViewAdapter(list, getApplicationContext()));
                                    }
                                });
                            }
                        });
                    }
                });
                thread.start();
            }
        });
    }

    private void initView() {
        rvSearchNews = (RecyclerView) findViewById(R.id.rvSearchNews);
    }
}